(function () {
	var $host;

	$host = $('[mag-thumb="inner"]');
	$host.mag({
	  toggle: true
	});

	$controls = $('[mag-ctrl="inner"]');
	$controls.magCtrl({
		mag: $host
	});

})();


(function () {
	var $host;

	$host = $('[mag-thumb="inner-inline"]');
	$host.mag();

})();


(function () {
	var $host;

	$host = $('[mag-thumb="outer"]');
	$host.mag({
	  mode: 'outer',
	  ratio: 1 / 0.7
	});
})();


(function () {
	var $host;

	$host = $('[mag-thumb="outer-drag"]');
	$host.mag({
	  mode: 'outer',
	  position: 'drag',
	  toggle: false
	});
})();


(function () {
	var $host;
	var $controls;
	var $hud;

	$host = $('[mag-thumb="drag"]');
	$host.mag({
	  position: 'drag',
	  toggle: false
	});

	$hudLeft = $('<div class="mag-eg-hud mag-eg-hud-left"></div>');
	$hudLeft.appendTo($host.parent());

	$hudRight = $('<div class="mag-eg-hud mag-eg-hud-right"></div>');
	$hudRight.appendTo($host.parent());

	var toPerc = function (p) {
		return (p * 100).toFixed(1) + '%';
	};

	$host.on('compute', function (e) {
		var mag = $(this).data('mag');
		var m = mag.model;
		$hudLeft.html(
			'<div>' + m.zoom.toFixed(1) + 'x</div>'
		);
		$hudRight.html(
			'<div>(' + toPerc(m.focus.x) + ', ' + toPerc(m.focus.y) + ')</div>'
		);
	});
})();
function classerror(){
	
	if($("#classname").val()==null||$("#classname").val()==""){
		$("#classerror").css("display","block");
		setTimeout(function () {
			$("#classerror").css("display","none");
	    }, 2500);
			return true;
	}
		return false;
}


(function () {
	var $host;
	var $controls;

	$host = $('[mag-thumb="controls"]');
	$host.mag({
		toggle: false,
		position: false
	});

	$controls = $('[mag-ctrl="controls"]');
	$controls.magCtrl({
		mag: $host
	});

})();
$(document).ready(function(){
	var classcolor;
	/*标题图片自动切换,和选择停止*/
	var num=0;
	var fors=self.setInterval(function(){
			num=num+1;
		var img=$("#minimg"+num);
		var src=img.attr("src");
		$("#min").attr("src",src);
		$("#bigimg").attr("src",src);
		if(num==4){
			num=0;
		}
	},2500);

	$(".minimg").hover(function(){
		var src=$(this).attr("src");
		$("#min").attr("src",src);
		$("#bigimg").attr("src",src);
	},function(){
		
	});
	$("#succes").hover(function(){
		clearInterval(fors);
	},function(){
		fors=self.setInterval(function(){
			num=num+1;
		var img=$("#minimg"+num);
		var src=img.attr("src");
		$("#min").attr("src",src);
		$("#bigimg").attr("src",src);
		if(num==4){
			num=0;
		}
	},2500);
		
	});
	/*增加或减少购买数量*/
	$("#reduce").click(function(){
		if(classerror()){
			return false;
		}
		var input=$("#num");
		var num=input.val();
		if(num<=1){
			input.val(1);
			return false;
		}
		input.val(num-1);
		
	});
$("#plus").click(function(){
		if(classerror()){
			return false;
		}
		var input=$("#num");
		var num=input.val();
		var stock=$("#stock").html();
		var newnum=Number(num);
		if(num>=stock){
			return false;
		}
		input.val(newnum+1);
		return false;
	});

$("#parameter").click(function(){
	$(this).css("background","#fff");
	$(this).css("color","#FF0000");
	$(this).css("border-bottom","none");
	$("#data").css("background","#EEDFCC");
	$("#data").css("border-bommon","1px solid #CDB79E");
	$("#data").css("color","#3B3B3B");
	$("#contentdiv").css("display","none");
	$("#parameterdiv").css("display","block");
	
});
$("#data").click(function(){
	$(this).css("background","#fff");
	$(this).css("color","#FF0000");
	$(this).css("border-bottom","none");
	$("#parameter").css("background","#EEDFCC");
	$("#parameter").css("border-bommon","1px solid #CDB79E");
	$("#parameter").css("color","#3B3B3B");
	$("#contentdiv").css("display","block");
	$("#parameterdiv").css("display","none");
	
});
$("#purchase").click(function(){
	var userid=$("#user").val();
	if(userid==""){
		$("#login").css("display","block");
	}else{
		if(classerror()){
			return false;
		}
		var stock=$("#stock").html();
		var nums=$("#num").val();
		var test=/[0-9]{1,20}/;
		if(nums>stock||nums==0){
			$("#num").val(1);
			alert("没有那么多货!");
			return false;
		} 
		if(!test.test(nums)){
			$("#num").val(1);
			alert("不允许的范围!");
			return false;
		}
		$("#commnum").val(nums);
		var vals=$("#cartform").serializeArray();
		var path=$("#path").val();
		$.post(path+"/CartController/addCart.action",vals,function(data){
			window.location.href=path+"/CartController/listCartByUser.action";
		});
	}
	
});
$("#cart").click(function(){
	var userid=$("#user").val();
	if(userid==""){
		$("#login").css("display","block");
	}else{
		if(classerror()){
			return false;
		}
		var stock=$("#stock").html();
		var nums=$("#num").val();
		var test=/[0-9]{1,20}/;
		if(nums>stock||nums==0){
			$("#num").val(1);
			alert("没有那么多货!");
			return false;
		} 
		if(!test.test(nums)){
			$("#num").val(1);
			alert("不允许的范围!");
			return false;
		}
		$("#commnum").val(nums);
		var vals=$("#cartform").serializeArray();
		var path=$("#path").val();
		$.post(path+"/CartController/addCart.action",vals,function(data){
		});
			alert("已添加至购物车");
	}
});
$("#closeLogin").click(function(){
	$("#login").css("display","none");
});
$("#submitform").click(function(){
	var form=$("#loginform").serializeArray();
	$.post("/mall/user/loginAjax.action",form,function(data){
		if(data.result==1){
			 location.reload();
		}else{
			$("#error").html(data.result);
		}
	});
	return false;
});
/*选择套餐分类*/
$(".colortype").click(function(){
	$(".colortype").each(function(){
		$(this).css("background","#CDC0B0");
	});
	$(this).css("background","#8B8B7A");
	classcolor=$(this).children("#cid").val();
	$("#price").html($(this).children("#cprice").val());
	$("#stock").html($(this).children("#cstock").val());
	$("#classid").val(classcolor);
	$("#classname").val($(this).children("#cname").html());
	$("#classprice").val($(this).children("#cprice").val());
	$("#fstock").val($(this).children("#cstock").val());
});
$("#carta").click(function(){
	var userid=$("#user").val();
	if(userid==""){
		$("#login").css("display","block");
		return false;
	}
});
/*自动填充颜色分类数据
 * $("#classid").val($("#colortype1").children("#cid").val());
$("#classname").val($("#colortype1").children("#cname").html());
$("#classprice").val($("#colortype1").children("#cprice").val());*/
});
